#include <XBee.h>

uint8_t option = 0;
uint8_t data = 0;
uint8_t payload[] = {
  0, 0, 0, 0, 0, 0};


XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
Tx16Request tx = Tx16Request(0x0001, payload, sizeof(payload));
TxStatusResponse txStatus = TxStatusResponse();

int blah = 0;

void setup(){
  xbee.setSerial(Serial2);
  xbee.begin(9600);
  Serial.begin(9600);
  xbee.send(tx);
}

void loop(){

  xbee.readPacket();

  if(xbee.getResponse().isAvailable()){
    Serial.println("GOT PACKET");
    if(xbee.getResponse().getApiId() == RX_16_RESPONSE){
      Serial.println("We got a message");
      xbee.getResponse().getRx16Response(rx16);
      Serial.print(rx16.getRemoteAddress16());
      Serial.println(" Is the address of the sending code");
      Serial.println("The data from the packet is: ");
      for(int i = 0; i < rx16.getDataLength(); i++){
        Serial.println(rx16.getData(i));
      }

      blah = random(40);
      for(int j = 0; j < sizeof(payload); j++){

        payload[j] = blah;
        blah ++;
      }
      delay(1000);
      xbee.send(tx);
    }

    if(xbee.getResponse().getApiId() == TX_STATUS_RESPONSE){
      xbee.getResponse().getTxStatusResponse(txStatus);

      if(txStatus.getStatus() == SUCCESS){
        Serial.println("WOOOT, TRANSFERED OUR CRAP!");
      }
      else{

        Serial.println("There seems to have been an error");
      }

    }
  }
}




